<?php
/*
 * Author: truonghm
 * Created date:11/11/2010
 * Package:user
 */
class User_LoginController extends Zend_Controller_Action{
/*
	 * Created by: truonghm;
	 * Created date: 29/10/2010
	 * Function: khoi tao cac gia tri thiet lap cua form 
	 */
	function init()
	{
		//$this->view->headTitle()->setSeparator(' / ');
		
		$this->view->title = Zend_Registry::get('translate')->translate("Header title");
   		$this->view->headTitle($this->view->title, 'PREPEND');
   		//print_r(Zend_Registry::get('translate'));
   		//echo Zend_Registry::get('translate')->translate("Header title");;
		//return;		
	}
	
	//
	public function indexAction(){
				
		$translate=Zend_Registry::get('translate');
				
		$form = new PhpExt_Form_FormPanel("loginForm");
		// Properties can be set by using the closures...
		$form->setBaseCssClass("x-plain")
		     ->setLabelWidth(100)
		     ->setUrl("/user/login/login/")
		     ->setDefaultType("textfield");
		
		$lblUsername = $translate->translate("Username");		
		$txtUsername = PhpExt_Form_TextField::createTextField("username",$lblUsername)
					 ->setAllowBlank(false);
		
		$lblPassword = $translate->translate("Password");
		$txtPassword = PhpExt_Form_PasswordField::createPasswordField("password",$lblPassword)
					 ->setAllowBlank(false);
					 
		$form->addItem($txtUsername, new PhpExt_Layout_AnchorLayoutData("100%")); // anchor width by percentage						
		$form->addItem($txtPassword, new PhpExt_Layout_AnchorLayoutData("100%"));		
		
		
		$onSubmit = PhpExt_Javascript::functionDef("onSubmit","login.getForm().submit({
                                        	method:'POST',
                                        	waitTitle:' ',                                        	
                                        	waitMsg:'".$translate->translate("Please wait!")."',
                                        	success:function(){                                        		
                                        		var redirect = '/dashboard/'; 
		                        				location = redirect;
		                        				window.hide();
                                        	},
	                                        failure:function(form, action){
	                                        	if(action.failureType == 'server'){
		                                        	obj = Ext.util.JSON.decode(action.response.responseText);
													Ext.Msg.alert('".$translate->translate("Notice")."', obj.errors.reason);
												}else{
													Ext.Msg.alert('".$translate->translate("Notice")."', '".$translate->translate("Enter the information")."');
												}
												login.getForm().reset();
											}							
										})");

		$form->setKeys(PhpExt_Javascript::variable('[{ key: [Ext.EventObject.ENTER], handler: function() {
											                    onSubmit();
											                }
											            }]'));
		
		$lblLogin = $translate->translate("Login");
		$btnLogin = PhpExt_Button::createTextButton($lblLogin)
				  ->setHandler(PhpExt_Javascript::variable('onSubmit'))
				  ->setId("btnLogin")
				  ->setName("btnLogin");

				  
		$window = new PhpExt_Window();
		$window->setTitle($lblLogin)
		       ->setWidth(350)
		       ->setHeight(140)
		       ->setMinWidth(350)
		       ->setMinHeight(140)
		       ->setLayout(new PhpExt_Layout_FitLayout())
		       ->setPlain(true)
		       ->setBodyStyle("padding:10px")
		       ->setButtonAlign(PhpExt_Ext::HALIGN_RIGHT);
		$window->addButton($btnLogin);
		$window->addItem($form);		
		
		$this->view->windowLogin = PhpExt_Ext::onReady(
			$onSubmit,
			$form->getJavascript(false, "login"),
			$window->getJavascript(false, "window"),
			$window->show()
		);
		$this->_helper->layout->disableLayout();
	}
	
	/*
	 * kiem tra thong tin dang nhap
	 */
	public function loginAction()
	{
		
		if($this->_request->isPost()){
			
			$translate=Zend_Registry::get('translate');
			
			$username = $this->_request->getParam ('username');
			$password = $this->_request->getParam ('password');
			
			$user = new User_Models_Business_User();		
			
			$userInfo["username"]= $username;
			$userInfo["password"] = $password;
						
			$result = $user->login(Zend_Json::encode($userInfo));
			
			$result = Zend_Json::decode($result, Zend_Json::TYPE_OBJECT);
						
			if($result->success){
				$flag["success"] = true;
			}
			else				
			{
				$flag["success"] = false;
				$flag["errors"]["reason"] = $translate->translate("Login failed. Try again.");				 
			}
		}
		echo Zend_Json::encode($flag);
		$this->_helper->layout->disableLayout();
	}
}		
	
	